using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using HLSSignature.Utils;

namespace HLSSignature
{
    internal partial class SigningCeremonyForm : Form
    {
        public bool success = false;
        private Microsoft.Office.Core.SignatureSetup sigsetup = null;
        private Microsoft.Office.Core.SignatureInfo siginfo = null;

        public SigningCeremonyForm(Microsoft.Office.Core.SignatureSetup sigsetup, Microsoft.Office.Core.SignatureInfo siginfo)
        {
            this.sigsetup = sigsetup;
            this.siginfo = siginfo;
            InitializeComponent();
        }

        private void SigningCeremony_Load(object sender, EventArgs e)
        {
            bool readOnly = this.sigsetup == null || this.siginfo == null || this.siginfo.ReadOnly;

            //this.combxPurpose.Enabled = !readOnly;
            this.txtSignature.Enabled = !readOnly;
            this.txtComments.Enabled =  !readOnly;
            this.hlinkSelectImage.Visible = !readOnly;
            this.btnChangeCert.Enabled = !readOnly;
            this.btnSign.Visible = !readOnly;

            if (this.sigsetup != null && this.siginfo != null)
            {
                this.lblSafeDisclaimer.Text = this.sigsetup.AdditionalXml;
                this.lblSignInst.Text = this.sigsetup.SigningInstructions;
                this.lblSignerInfo.Text = this.sigsetup.SuggestedSigner;
                this.lblSignerTitle.Text = this.sigsetup.SuggestedSignerLine2;
                //this.lblSignerInfo.Text = this.siginfo.GetSignatureDetail(Microsoft.Office.Core.SignatureDetail.sigdetDelSuggSigner) + "\\n" +
                //        this.siginfo.GetSignatureDetail(Microsoft.Office.Core.SignatureDetail.sigdetDelSuggSignerLine2);
                this.txtComments.Text = this.siginfo.SignatureComment;
                this.lblCertNameValue.Text = (string)this.siginfo.GetCertificateDetail(Microsoft.Office.Core.CertificateDetail.certdetSubject);
                this.lblCertIssuerValue.Text = (string)this.siginfo.GetCertificateDetail(Microsoft.Office.Core.CertificateDetail.certdetIssuer);
                if (this.siginfo.SignatureText.Length > 0)
                {
                    this.txtSignature.Text = this.siginfo.SignatureText;
                    this.pictbxSig.Visible = false;
                }
                else
                {
                    this.pictbxSig.Image = AxHost2.GetPictureFromIPicture(this.siginfo.SignatureImage);
                    this.txtSignature.Visible = false;
                }
                if (readOnly)
                {
                    //this.combxPurpose.Text = this.siginfo.SignatureComment;
                    
                    
                    this.lblSignTime.Text = this.siginfo.GetSignatureDetail(Microsoft.Office.Core.SignatureDetail.sigdetLocalSigningTime).ToString();
                    this.btnChangeCert.Enabled = false;
                    this.btnCancel.Text = "Close";
                }
                else
                {
                   
                    this.pictbxSig.Visible = false;
                    this.txtSignature.Visible = true;
                    //this.pictbxSig.Image = AxHost2.GetPictureFromIPicture(this.siginfo.SignatureImage);
                    this.lblSignTime.Text = DateTime.Now.ToString();
                    this.btnSign.Enabled = false;
                }
            }
            //else
            //{ 
                
            
            //}
        }
        private void txtSignature_TextChanged(object sender, EventArgs e)
        {
            this.btnSign.Enabled = (this.txtSignature.Text.Length > 0);

        }
        private void hlinkSelectImage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.sigsetup == null || this.siginfo == null || this.siginfo.ReadOnly)
                throw new InvalidOperationException();

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog(new HwndWrapper(this.Handle)) == DialogResult.OK)
            {
                try
                {
                    this.pictbxSig.Image = Image.FromFile(ofd.FileName);
                    this.pictbxSig.Visible = true;
                    this.txtSignature.Visible = false;
                    this.btnSign.Enabled = true;
                }
                catch
                {
                    MessageBox.Show(new HwndWrapper(this.Handle), "Invalid image", "Signing Ceremony", MessageBoxButtons.OK);
                }
            }
        }

        private void btnChangeCert_Click(object sender, EventArgs e)
        {
            if (this.sigsetup == null || this.siginfo == null || this.siginfo.ReadOnly)
                throw new InvalidOperationException();

            this.siginfo.SelectSignatureCertificate(new HwndWrapper(this.Handle));
            this.lblCertNameValue.Text = (string) this.siginfo.GetCertificateDetail(Microsoft.Office.Core.CertificateDetail.certdetSubject);
            this.lblCertIssuerValue.Text = (string) this.siginfo.GetCertificateDetail(Microsoft.Office.Core.CertificateDetail.certdetIssuer);
        }

        private void btnViewCert_Click(object sender, EventArgs e)
        {
            if (this.sigsetup == null || this.siginfo == null)
                throw new InvalidOperationException();

            this.siginfo.ShowSignatureCertificate(new HwndWrapper(this.Handle));
        }

        private void btnSign_Click(object sender, EventArgs e)
        {
            if (this.sigsetup == null || this.siginfo == null || this.siginfo.ReadOnly)
                throw new InvalidOperationException();

            this.siginfo.SignatureComment = this.txtComments.Text;
            if (this.txtSignature.Visible)
            {
                this.siginfo.SignatureText = this.txtSignature.Text;
            }
            else
            {
                this.siginfo.SignatureImage = AxHost2.GetIPictureDispFromPicture(this.pictbxSig.Image);
            }

            this.success = true;
            this.Close();
        }

       
    }
}